<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/10
 * Time: 14:19
 */
namespace Huangzikun\hawkeye_spider;

class NginxSpiderClass extends BaseClass
{
    public $spiderInfo;
    public $apiAddress;
    public $uri;

    public function __construct($apiAddress)
    {
        parent::__construct($apiAddress);
    }

    public function grab()
    {
        $context = stream_context_create(array(
            'http' => array(
                'timeout' => 3 //超时时间，单位为秒
            )
        ));
        $nginxStatus = file_get_contents($this->uri, false, $context);
        if(empty($nginxStatus)){
            return ;
        }

        $nginxStatus = explode(PHP_EOL, $nginxStatus);
        $active = explode(":", $nginxStatus[0]);
        $this->spiderInfo['active_connections'] = intval($active[1]);

        $num = explode(" ", $nginxStatus[2]);
        $this->spiderInfo['server'] = intval($num[1]);
        $this->spiderInfo['accepts'] = intval($num[2]);
        $this->spiderInfo['handled_requests'] = intval($num[3]);

        $readAndWrite = explode(" ", $nginxStatus[3]);
        $this->spiderInfo['reading'] = $readAndWrite[1];
        $this->spiderInfo['writing'] = $readAndWrite[3];
        $this->spiderInfo['waiting'] = $readAndWrite[5];
    }
}